import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:liuxlcommon/util/string_utils.dart';
import 'package:liuxlcommon/view/loading_view.dart';
import 'package:liuxlcommon/view/lxl_widget.dart';
import 'package:liuxlcommon/view/my_box.dart';
import 'package:liuxlcommon/view/my_style.dart';
import 'package:love_newf/app/com/base/base_state.dart';
import 'package:love_newf/app/com/model/friend_user_info_model.dart';
import 'package:love_newf/app/com/view/label/province_label.dart';
import 'package:love_newf/app/com/view/label/sex_label.dart';
import 'package:love_newf/app/com/view/label/vip_grade_label.dart';
import 'package:love_newf/app/page/chat/chat_view.dart';
import 'package:love_newf/value/app_colors.dart';
import 'package:love_newf/value/app_const.dart';

import 'search_logic.dart';

class SearchPage extends StatefulWidget {
  const SearchPage({super.key});

  @override
  State<SearchPage> createState() => _SearchPageState();
}

class _SearchPageState extends BaseState<SearchPage, SearchLogic> {
  @override
  get initData {
    title = "搜索";
  }

  @override
  Widget get easyBody => LColumn.eazy([searchBox, LExpand(child: searchList)])
      .onTapDown((d) => OldLInput.unfocus())
      .size(double.infinity, Get.height)
      .bgColor(Colors.white);

  get searchBox => LRow.eazy([
        8.lsw,
        const Icon(Icons.search_outlined),
        LInput(
          logic.keyword,
          lStyle: LStyle(
              height: 52.w,
              maxLength: 4,
              alignment: Alignment.centerLeft,
              flex: 1,
              fontSize: 14.sp,
              hintColor: AppColors.textHintColor,
              hintText: '输入ID或者昵称'),
          onChanged: (text) {
            logic.keyword = text;
            logic.update();
          },
          onSubmitted: (text) {
            logic.datas = null;
            logic.update();
          },
        ),
        LText.easy("搜索   ", 14.w, fw6, Color(0xFFFF5E9B)).onTapL(() {
          logic.datas = null;
          logic.update();
        })
      ])
          .paddingV(5.w)
          .bgColor(Color(0xFFF5F4F7))
          .radius(36.w)
          .size(353.w, 36.w);

  get searchList => LoadingView(
        datas: logic.datas,
        itemCall: (context, position) => itemView(position),
        isPullUp: false,
        isPullDown: false,
        onStateClick: (type) {
          // if ((logic.datas?.length ?? 0) == 0) logic.datas = null;
          // logic.update();
        },
        onLoad: (type) async {
          return await logic.getData();
        },
        onSuccess: () {
          if (logic.datas != null) logic.update();
        },
      );

  itemView(position) {
    FriendUserInfoModel item = logic.datas![position];
    return LColumn(
        lStyle: LStyle(

            margin: EdgeInsets.fromLTRB(11.w, 10.w, 11.w, 0),
            radius: 16.w,
            padding: EdgeInsets.symmetric(vertical: 13.w, horizontal: 10.w),
            backgroundColor: Colors.white,
            boxShadow: [
              const BoxShadow(blurRadius: 8, color: Color(0x1A000000))
            ]),
        children: [
          LRow.eazy([
            LImage.easy(item.avatar, 66.w, 66.w, 66.w),
            12.lsw,
            LColumn.eazy([
              LText.easy(item.nick, 16.w, fw5),
              3.lsh,
              LRow.eazy([
                SexAgeLabel(item.sex, item.age),
                4.lsw,
                ProvinceLabel(item.province),
                4.lsw,
                VipGradeLabel(item.loversGrade),
                4.lsw,
                LText.easy(" ID ", 11.w, fw8, Colors.white)
                    .radius(6.5.w)
                    .bgColor(const Color(0xFFCED9EA)),
                LText.easy(item.userCode, 12.w, fw8, const Color(0xFF9E9E9E))
              ]),
              10.lsh,
              LText.easy(
                  StringUtils.defaultValue(item.introduction, "这个人很懒,什么都没写..."),
                  12.w,
                  fw4,
                  const Color(0xFFAAAAA8)),
            ]).align(Alignment.topLeft).flex(1)
          ]),
          20.lsh,
          LRow(lStyle: LStyle(spaceBetween: 20.w), children: [
            LText.easy("私聊", 16.w, fw4, Colors.white).onTapL((){
              ChatPage.openPage(item.userId, friendUserInfo: item);
            })
                .radius(36.w)
                .gradient(const [Color(0xFFFF8634), Color(0xFFFF3B82)],
                    Alignment.centerRight)
                .size(90.w, 36.w)
                .center(),
            if (item.living == 1)
              LText.easy("观看直播", 16.w, fw4, Colors.white)
                  .radius(36.w)
                  .gradient(const [Color(0xFF7D7EFF), Color(0xFFF561FF)],
                      Alignment.centerRight)
                  .size(90.w, 36.w)
                  .center()
          ])
        ]);
  }
}
